package com.gmako.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gmako.dao.BaseDAO;
import com.gmako.entity.Adress;
import com.gmako.service.AdressManageService;
import com.gmako.utils.Pager;
@Service
public class AdressManageServiceImpl implements AdressManageService{
	@Autowired
	private BaseDAO baseDAO;

	/**
	 * 添加服装信息
	 * @param obj
	 */
	public void save(Object obj) {
		baseDAO.save(obj);	
	}

	/**
	 * 查询所有分页服装信息
	 * @param pageSize
	 * @param pageNo
	 * @param clazz
	 * @return
	 */
	public Pager findPagerAll(int pageSize, int pageNo,Class clazz) {
		return baseDAO.findPagerAll(pageSize, pageNo, clazz);
	}

	/**
	 * 根据id删除服装信息
	 * @param clazz
	 * @param id
	 */
	public void delete(Class clazz, Serializable id) {
		baseDAO.deleteById(clazz, id);		
	}
	
	/**
	 * 根据属性查询服装信息
	 * @param clazz
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public List findByProperty(String clazz, String propertyName,
			Object value) {
		return (List) baseDAO.findByProperty(clazz, propertyName, value);
	}

	/**
	 * 修改服装信息
	 * @param obj
	 */
	public void update(Object obj ) {
		baseDAO.update(obj);		
	}
	
	/**
	 * 根据分页查询地址
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public Pager findTypes(int pageSize, int pageNo) {
		String sqlCount ="select count(*) from Adress z left JOIN Adress f on z.pid=f.id";
		String SqlList ="select z.id,(SELECT adressName FROM Adress where id=z.id) zname,(SELECT adressName FROM Adress where id=f.id) fname from Adress z left JOIN Adress f on z.pid=f.id ORDER BY id";
		return baseDAO.findPagerSqlForJoin(pageSize, pageNo, sqlCount, SqlList);
	}

	public int worksCountByUid(int uid) {
		return baseDAO.countAll("com.gmako.entity.Clothings where u_Id = "+uid);
	}

//
//	public Clothings getClothingsById(int id) {
//		return (Clothings) baseDAO.loadObject("from com.gmako.entity.Clothings where id = "+id);
//	}
//
//	public int editClothing(Clothings clothings) {
//		return baseDAO.update("update com.gmako.entity.Clothings set sortIndex = "+clothings.getSortIndex()+" where id="+clothings.getId());
//	}
//	
	
	/**
	 * 根据作品id返回作品信息 
	 * 2012-8-24上午8:54:11
	 * @param pageSize
	 * @param pageNo
	 * @param id
	 * @return Pager
	 */
	public List getClothing(int id){
		String sqlList = "select (select typeName from clothingtype where id=clothings.clothingType) fztype ,(select typeName from clothingtype where id=clothings.subcategories) fzztype,clothings.colorClass,(select count(id) FROM clothingshare where clothingshare.c_Id=clothings.id) renqi,(select nickName from users where id=clothings.u_Id) shejishi,clothings.id,clothings.title,clothings.u_Id,clothings.workdescription,clothings.commend,clothings.description,clothings.price,(select fabric.name from fabric where  id=clothings.fabric) mianliao,(select fabriccompositioncontent.content from fabriccompositioncontent where id=clothings.FabricContent) mianliaob,clothings.fNo  from clothings,clothingtype where clothings.id="+id+" and clothings.clothingType=clothingtype.id ";
		return baseDAO.findBysql(sqlList);
	}
	/**
	 * 获得推荐作品 
	 * 2012-8-24上午8:54:11
	 * @param id
	 * @return Pager
	 */
	public List getCommendClothing(String id){
		String sqlList = "select * from clothings where clothings.fNo in('"+id+"')";
		return baseDAO.findBysql(sqlList);
	}
	/**
	 * 获得颜色 
	 * 2012-8-24上午8:54:11
	 * @param id
	 * @return Pager
	 */
	public List getColors(String id){
		String sqlList = "select colors.name from colors where id in("+id+")";
		return baseDAO.findBysql(sqlList);
	}
	
	
	
	/**
	 * 查询服装分类是否重名
	 * @param name
	 */
	public int findTypeIs(String name){
		String hql = "Select count(id) from Adress where adressName= '"+name+"'";
		long user = (Long)baseDAO.loadObject(hql);
		if(user==0){
			return 0;
		}
		else
			return 1;
	}
}
